Beginners mind (Shoshin)

Ben Whalley, Paul Sharpe, Sonja Heintz

Beginners mind (Shoshin) denotes openness, eagerness and lack of preconceptions when studying a subject, just as a beginner would, no matter what level of expertise the student has.

Even black belt martial artists practice basic techniques like blocks and punches every time they train.

This session doesn’t assume any prior knowledge of R, and introduces the basics. For some this will be revision from last year, but we provide additional material for advanced students test their knowledge and extend familiar skills. Even if you are quite confident with R Studio from Stage 1, please read the worksheet carefully and complete all of the activities in the blue boxes.

General principles

  • Reproducibility and transparency in science (as a motivation for using R)
  • Precision and attention to detail as an important skill.

Using the RStudio interface

These worksheets assume that you are using a web browser to access the RStudio Server at Plymouth University.

NOTE: RStudio works on most web browsers (e.g. Firefox, Safari, Chrome) but does not work that well on the default web browser in Windows 10 (“Edge”). If you’re using Windows, we recommend downloading Firefox and using that. Firefox is free and open source.

When you login to RStudio, you’ll be greeted with a screen that looks something like the image below.

RStudio on first opening

When you open RStudio for the first time, you can see three parts:

  1. The Console - This is the large rectangle on the left. This is where you tell R what to do, and it’s also where R prints the answers to your questions.

  2. The Environment - This is the rectangle on the top right. This is where R keeps a list of the data it knows about. It’s empty at the moment, because we haven’t given R any data yet.

  3. The Files - This is the rectangle on the bottom right. This is a bit like the File Explorer in Windows, or the Finder on a Mac. It shows you what files and folders R can see.

You should also be able to see that the two rectangles on the right have a number of other “tabs.” These work like tabs on a web browser.

The top rectangle has the tabs “Environment” and “History.” The History tab keeps a record of commands you’ve recently typed into the Console. This can sometimes be useful.

The bottom rectangle has the tabs “Files,” “Plots,” “Packages,” “Help,” and “Viewer.” We’ll cover what these other tabs do later on.

Before you start

Before starting this module, you need to run an R command which makes a folder and downloads the files you will need for each workshop.

  1. Click on the Console pane
  2. Copy-paste the following command into the console

source('https://raw.githubusercontent.com/benwhalley/lifesavR/main/bootstrap.R')

Your console should now look like this:

Press return (enter) to run the command. If your console looks like the image below, then you are ready to start the session.

What can R do?

Watch the following short video to see a few things that R can do.

#todo #video of what R can do to provide context for where we’re going and what we’re doing.

RStudio is a user interface to R, which is a computer language that is primarily designed for data analysis and visualisation. R is a text-based language, so you interact with it by typing commands, and then running those commands with R. R Studio makes it easy to run R commands and organise your work. For example, you can do simple arithmetic (2 * 221), generate some random numbers (rnorm(10, 0,1)), and plot some random numbers (hist(rnorm(100, 0,1)).

You should think of R as a robot. The robot is extremely fast, powerful and tireless, but it’s also literal-minded, and won’t think for itself or take the initiative. You need to tell it exactly what to do, by providing very precise instructions.

Working interactively in R Markdown

Click on the lifesavr folder in the Files pane. Notice than some files have the extension .rmd. These are R Markdown files. It is important that any R Markdown file you create has the extension .rmd (or .Rmd), because this is how RStudio knows what they contain.

R Markdown is a way of combining R with narrative text. It allows you to integrate the results of your data analysis into high quality reports, research papers, dissertations or books. Because it’s such a powerful tool, this module provides an early, gentle introduction to R Markdown.

RStudio needs to distinguish R code from narrative text. This is done by putting the code inside some special characters, creating what’s referred to as a chunk. A chunk is opened using the symbols ```{r}, and closed using the symbols ```. This is what a chunk looks like in RStudio (this chunk has been given the optional name life):

A code chunk in the RMarkdown editor

NOTE: The symbols which start and end a chunk are backticks, not single quotes.

On windows

On a Mac

Running R code within a chunk

Watch the following short video to see how to run code within a chunk.

#todo re-record #video running code in a chunk.

In each session you will work in a single R Markdown file, which we will refer to as a workbook. The file you need for this session is called session-1.rmd.

Click on session-1.rmd in the Files pane.

As the video shows, one way to run code within a chunk is to execute the commands one at a time, using the following keys:

Windows, Linux: Ctrl +

Mac: +

  1. Locate the first chunk in session-1.rmd
  2. Place your cursor (anywhere) on the line that says library(tidyverse)
  3. Run the commands by pressing Ctrl + (Windows, Linux) or + ↩︎ (Mac)

You will see some output appear beneath the chunk. Don’t worry about the details for now, we’ll explain those later. However, one of the effects of the command you have just run is to load some data about diamonds.

Now position your cursor on the line that says diamonds and run the commands.

You should see the a scatterplot of the diamonds data appear below the chunk:

Congratulations! You have just run your first lines of R. The code to produce the plot consisted of three lines. You can also run part of a line by highlighting the code you want to run:

  1. Select (highlight) the word diamonds
  2. Run the code

This prints the first few lines of the diamonds data:

Example of running highlighted code

Why would you want to run part of a line of code? In these workshops you will combine simple steps into sequences which do a particular job, such as generating a plot. It’s natural, especially when you’re new to R, that the full sequence of commands won’t do exactly what you want first time. Running part of your code allows you to identify the steps which are correct. This allows you to modify subsequent steps until your code produces the required results. Remember this technique as you will be using it extensively in these workshops.

Variables

Using variables lets us store calculations for later. A variable is a name which can be assigned a value using the assignment operator: <-.

Run the two lines in the chunk named life.

The results should look like this:

Results of running life chunk

Line 24 runs the calculation 40 + 2, then assigns the result to the variable meaningoflife. The assignment operator <- looks like an arrow that points to the left. This is a reminder that the results of the calculation on the right hand side will be assigned to the variable on the left hand side. Line 25 displays the value of meaningoflife.

Variables that you create are stored in what’s called the Global Environment. You can see them in the Environment pane.

Variables that you create are stored in the Global Environment

Inserting a chunk

You insert a new chunk by positioning your cursor on the line where you want the chunk to appear, and selecting the Code > Insert Chunk menu option:

Insert a new chunk

There are also keyboard shortcuts for inserting a chunk:

Windows, Linux: Ctrl + Alt + I

Mac: + I

Exercise 1

  1. Find the instructions for Exercise 1 in your workbook
  2. Create a new chunk below the instructions
  3. Inside the chunk, write a line of code which adds together the numbers 9, 4, 55 and 2, and assigns the result to a variable named sum.
  4. Run the the line of code you have written

After completing these steps, your environment should look like this:

Environment after Exercise 1

Loading packages

A powerful feature of R is that it can be extended to analyse or plot data in any way imaginable. A package (sometimes called a library) is an extension to R that adds new commands. Packages are loaded using the library() command.

The first command you ran above was library(tidyverse). This loaded the commands needed to create the scatterplot, and also the diamonds data. The tidyverse package is so fundamental to this course that library(tidyverse) is likely to be the first line of R in the first chunk of each of your R Markdown files.

If you’ve understood what packages are then it should be clear that you can’t use the commands provided by tidyverse (and the additional packages it loads) until you’ve run the command library(tidyverse).

For example, if you tried to produce the scatterplot before loading tidyverse you’d see an error like this in the console:

Error in diamonds %>% ggplot(aes(carat, price, colour = clarity)) : 
  could not find function "%>%"

We mention this here, as could not find function errors are one of the most common problems that beginners encounter. They normally mean that you have

  1. forgotten to include library(tidyverse) as the first line in your code, or
  2. forgotten to run that line.

Built-in datasets

A dataset is a set of data relating to a particular topic. Most datasets we will be working with consist of rows and columns, just like a spreadsheet. In R this type of data is stored in a special type of variable called a data.frame. You will also see references to datasets as tibbles. A tibble is just a special type of data.frame, so you can treat the two types of variable as being equivalent.

R has a number of built-in datasets, and more can be loaded from packages.

#todo #video of built-in datasets.

One data.frame that is built-in to R is called mtcars. This is a dataset about cars that was published in a US magazine called Motor Trend. Let’s display this data in using a new chunk. As we did with the diamonds tibble, if we type mtcars, select the variable name and execute it, we can see the data it contains.

By default this displays only the first ten rows and columns of the data. You can see other rows using the Next, Previous and number buttons below the data. You can see additional columns using the arrow next to the final, right-hand column.

You already know that the diamonds dataset was loaded using library(tidyverse). The gapminder package includes a tibble that contains data about life expectancy, GDP per capita, and population by country. We can load and explore this dataset in a the same way we loaded diamonds dataset. We load the gapminder package, type the name of the tibble (also gapminder) and run it. Again, we can use the navigation buttons to explore the data.

Try this out in your workbook:

  1. Create a new chunk at the bottom of your worksheet
  2. Display the mtcars data.frame and try out the navigation buttons
  3. Load the gapminder package, display the gapminder tibble and explore the data

Exploring and checking data

#todo #video of built-in datasets.

The glimpse command

Using the glimpse command we can have the columns of a dataset run down the page, and data run across. The command mtcars %>% glimpse() demonstrates this. We’ll explain what the %>% command means shortly. This is like rotating the output you saw earlier anti-clockwise by 90 degrees. The command displays as many observations from the dataset as will fit on a single line.

Each column in a data.frame should be thought of as a variable. Variables have an associated type. When you assigned a number to a variable earlier, behind the scenes, R assigned the variable as numeric. glimpse is useful as the second column in the output shows us the type of each variable. As you can see, all variables in mtcars have the type dbl. This is short for ‘double-precision number’; for now, just know that dbl means a number.

We can see other variable types using glimpse to display the gapminder data.

  • int is short for ‘integer,’ a variable which contains whole numbers (e.g. a participant id number)
  • fct is short for ‘factor,’ a categorical variable (e.g. a specific response to a multiple-choice question)

Other types include:

  • chr — short for ‘character,’ a variable which contains text (e.g. an email address), and
  • ord — short for ‘ordered’; a variant of fct where the categories have a particular order (e.g. responses like ‘Wost’ < ‘Better’ < ‘Best’)

We’ll return to why it’s important to know the types of your variables shortly.

Exercise 2

  1. Create a new chunk at the bottom of your worksheet
  2. Use glimpse to display the mtcars, gapminder and diamonds datasets

Now use the output from Exercise 2 to answer the following question. After entering your answer, click outside the box. The border will turn turn blue when the answer is correct.

  • The clarity variable in the diamonds tibble is of type

clicking on the Environment window to look at data

There is a menu which allows you to view your variables as a list or a grid. Grid view shows you the type in the Type column.

Environment with list/grid menu

ggplot and the pipe

Scatter plots

  • the pipe %>% sends data to the next bit of code

  • Introduce/recall ggplot

    • demonstrate geom_point and geom_jitter
      • TODO find an example dataset which requires geom_jitter because on integer scale
# TODO find better example for this
attitude %>%
  ggplot(aes(rating, complaints)) +
  geom_point()


# vs.

attitude %>%
  ggplot(aes(rating, complaints)) +
  geom_jitter()

Boxplots

gapminder::gapminder %>%
  ggplot(aes(continent, lifeExp)) +
  geom_boxplot()

Problems with x axes

Show this and highlight it’s not what we expect

mtcars %>%
  ggplot(aes(am, mpg)) +
  geom_boxplot()
Warning: Continuous x aesthetic -- did you forget aes(group=...)?

The reason is visible here:

mtcars %>% glimpse()
Rows: 32
Columns: 11
$ mpg  <dbl> 21.0, 21.0, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8, 19.2, 17.8,…
$ cyl  <dbl> 6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 8,…
$ disp <dbl> 160.0, 160.0, 108.0, 258.0, 360.0, 225.0, 360.0, 146.7, 140.8, 16…
$ hp   <dbl> 110, 110, 93, 110, 175, 105, 245, 62, 95, 123, 123, 180, 180, 180…
$ drat <dbl> 3.90, 3.90, 3.85, 3.08, 3.15, 2.76, 3.21, 3.69, 3.92, 3.92, 3.92,…
$ wt   <dbl> 2.620, 2.875, 2.320, 3.215, 3.440, 3.460, 3.570, 3.190, 3.150, 3.
$ qsec <dbl> 16.46, 17.02, 18.61, 19.44, 17.02, 20.22, 15.84, 20.00, 22.90, 18…
$ vs   <dbl> 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,…
$ am   <dbl> 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0,…
$ gear <dbl> 4, 4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3, 3,…
$ carb <dbl> 4, 4, 1, 1, 2, 1, 4, 2, 2, 4, 4, 3, 3, 3, 4, 4, 4, 1, 2, 1, 1, 2,…

The am has type dbl

We must tell R it’s a factor:

mtcars %>%
  ggplot(aes(factor(am), mpg)) +
  geom_boxplot()

Adding color. This works:

diamonds %>%
  ggplot(aes(carat, price, colour = clarity)) +
  geom_point()

This doesn’t so well:

mtcars %>%
  ggplot(aes(wt, mpg, color = cyl)) +
  geom_point()

We can improve it like this:

mtcars %>%
  ggplot(aes(wt, mpg, color = factor(cyl))) +
  geom_point()

Introduction to Markdown]

Check your knowledge

  • What is mtcars?
  • Explain what glimpse does
  • What is the %>% symbol called and what does it do?
  • What is the <- symbol called and what does it do?
  • What is the difference between a dbl and an ord/fct?
  • Give an example of when the difference between dbl and fct matters when making a plot
  • How can you convert a variable from a dbl to a fct
  • What is the difference between geom_jitter() and geom_point()?
  • Why is geom_jitter useful sometimes?

Extensions

  • Lots more practice plots with different datasets?
  • Better plotting worksheet stage 4?

Further reading